from first_data import *
import math

#通过遍历下半部分图像找到最高点
def get_top(img,h,w):
    for i in range(1,w):
        for j in range(int(h/2),h):
            if img[i,j] == 255:
                return i,j

def get_number(top_w,top_h,img,h,w):

    '''
    #方法一
    #这里面应用了一个数学计算来计算偏航角度，然后/180来归一，在*100得到最终的结果
    #先求斜率，因为这里是正比例函数
    k = (abs(h - top_h)/abs((w/2) - top_w))
    print(k)
    #arctan求角度
    angle = (math.atan(k))
    end_angle = 90 - angle
    print(end_angle)
    #量化
    go_number = (end_angle/90) * 50
    if top_w <= (w/2) :
        go_number = go_number
    else:
        go_number = 50 +  go_number
    return go_number
    '''

    #方法二
    diff = abs(int(w/2) - top_w)
    end_number = (diff / int(w/2)) * 50
    if top_w > int(w/2):
        return int(50 + end_number)
    else:
        return int(50 - end_number)

def get_direction(img_target,img):
    #获取图像大小
    h,w = img_target.shape
    print(h,w)
    top_h,top_w = get_top(img_target, h, w)
    print(top_w,top_h)
    cv2.circle(img, (top_w, top_h), 5,(255,255,255))
    cv2.line(img, (int(w/2), h), (top_w, top_h), (255, 0, 0), 5)
    #cv2.imshow("3",img)
    #cv2.waitKey(0)
    go_number = get_number(top_w, top_h, img, h, w)
    print(go_number)
    return go_number
